package com.cherish.web.driver.controller;

import com.alibaba.fastjson.JSONObject;
import com.cherish.utils.Message;
import com.cherish.utils.ResultUtils;
import com.cherish.utils.ResultVo;
import com.cherish.web.driver.entity.Driver;
import com.cherish.web.driver.service.DriverService;
import com.cherish.web.menu.entity.MakeMenuTree;
import com.cherish.web.menu.entity.Menu;
import com.cherish.web.user.entity.RouterVO;
import com.cherish.web.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author:MQ
 * Date:2024/1/513:18
 */
@Api(tags = "司机管理")
@RestController
@RequestMapping("/api/driver")
public class DriverController {
    @Autowired
    private DriverService driverService;

    @ApiOperation(value="获取司机列表")
    @PostMapping("/driverList")
    public ResultVo getDriverList(HttpServletRequest request, HttpServletResponse response){
        // 获取用户相关的信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 获取用户信息
        User user = (User) authentication.getPrincipal();
        // 2. 获取前台查询内容
        JSONObject params = JSONObject.parseObject(request.getParameter("query"));
        // 3. 获取司机列表
        List<Driver> driverList = null;
        driverList = driverService.getDriverListByUsername(user,params);
        return ResultUtils.success("获取成功",driverList);
    }
    @ApiOperation(value="获取司机详情/默认值")
    @PostMapping("/driverInfo")
    public ResultVo getDriverInfo(HttpServletRequest request, HttpServletResponse response){
        // 获取用户相关的信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 获取用户信息
        User user = (User) authentication.getPrincipal();
        // 2. 获取前台查询内容
        String fid = request.getParameter("fid");
        // 3. 获取司机列表
        Driver driverInfo = null;
        driverInfo = driverService.getDriverInfoById(user,fid);
        return ResultUtils.success("获取成功",driverInfo);
    }

    @ApiOperation(value="删除司机")
    @PostMapping("/driverRemove")
    public ResultVo removeDriver(HttpServletRequest request, HttpServletResponse response){
        // 获取用户相关的信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 获取用户信息
        User user = (User) authentication.getPrincipal();
        // 2. 获取前台查询内容
        String fid = request.getParameter("fid");
        // 3. 获取司机列表7
        Message message = null;
        message = driverService.removeDriverById(user,fid);
        return ResultUtils.success("删除成功",message);
    }
}
